var CANDIDATE = {
		currentId : '0',
		loading: '',
		actionName: '',
		actionNameJob: '',
		actionNameDelete: '',
		currentLanguge: '',
		actionNameInterview: '',
		init: function(){
			$('tr.trdetail').hide();
			$('a.get-detail').unbind('click').click(function(){
				var $tr = $(this).parent().parent().next();
				var $divDetail = $('div.candidate-detail-information', $tr);
				var $divSeach = $('div.job-search', $tr);
				if(parseInt(CANDIDATE.currentId, 10) == parseInt($(this).attr('data-id'))){
					$tr.hide('fast');
					CANDIDATE.currentId = '0';
				}else{
					if(parseInt(CANDIDATE.currentId) != 0){
						$('tr.dtr_'+CANDIDATE.currentId).hide('fast');
					}
					CANDIDATE.currentId = $(this).attr('data-id');
					
					$divDetail.html(CANDIDATE.loading);
					$divSeach.html('').hide();
					$tr.show('fast');
					CANDIDATE.loadCandidateDetail($divDetail);
				}
			});
			
			$('a.ajax-search-job').unbind('click').click(function(){
				CANDIDATE.searchJob();
				return false;
			});
			$('a.ajax-load-interview').unbind('click').click(function(){
				CANDIDATE.loadInterview();
				return false;
			});
			CANDIDATE.addToExport();
			CANDIDATE.loadHistory();
			CANDIDATE.loadApply();
			
		},
		loadHistory: function(){
			$('a.ajax-history').unbind('click').click(function(){
				
				var $ob = $(this);
				$.ajax({
					type: "POST",
					url: $ob.attr('data-url'),
					data: {
						id: $ob.attr('data-id'),
						action_name: $ob.attr('data-action'),
					},
					cache: false,
					success: function(data){
						$.fancybox.open(data,{
							padding : 10,
							title: $ob.attr('title'),
							helpers: {
								overlay:{
									closeClick : false
								} 
							}
						});
						CANDIDATE.applyMaster();
					}
				});
				
			});
		},
		loadApply: function(){
			$('a.ajax-apply').unbind('click').click(function(){
				
				var $ob = $(this);
				$.ajax({
					type: "POST",
					url: $ob.attr('data-url'),
					data: {
						id: $ob.attr('data-id'),
						action_name: $ob.attr('data-action'),
					},
					cache: false,
					success: function(data){
						$.fancybox.open(data,{
							padding : 10,
							title: $ob.attr('title'),
							helpers: {
								overlay:{
									closeClick : false
								} 
							}
						});
						$('.date-picker').datepicker();
						var APPLY_ISLOADING = false;
						$('.update-job').unbind('change').change(function(){
								if (APPLY_ISLOADING) return false;
								APPLY_ISLOADING = true;
								$.ajax({
									type: "POST",
									url: '/admin/candidates/candidateApply',
									data: {
										company_id: $('#apply-company-id').val(),
										search_key: $('#apply-search-key').val(),
										date_from: $('#apply-date-from').val(),
										date_to: $('#apply-date-to').val(),
										id: $('#apply-id').val(),
										action_name: '_apply_search'
									},
									cache: false,
									success: function(data){
										APPLY_ISLOADING = false;
										var $data = $.parseJSON(data);
										if($data.status == 1){
											var $str = '<tr><th class="job-name">Công việc</th>	<th class="action">Ứng tuyển</th><th class="action">Ghi chú</th><tr>';
											for(var $i = 0; $i < $data.data.length; $i++){
												if($data.history.indexOf($data.data[$i].id) >=0 ){
													$str +='<tr class="-applied"><td>'+$data.data[$i].Companies.company_name + ' - ' + $data.data[$i].title+'</td><td><a href="#" class="apply-insert" data-title="Ứng tuyển" data-candidate-id="'+$data.id+'" data-job-id="'+$data.data[$i].id+'" title="Ứng tuyển"><img src="/css/admin/img/global/control-enable.png"></a></td><td>Đã ứng tuyển</td></tr>';
												}else{
													$str +='<tr><td>'+$data.data[$i].Companies.company_name + ' - ' + $data.data[$i].title+'</td><td><a href="#" class="apply-insert" data-title="Ứng tuyển" data-candidate-id="'+$data.id+'" data-job-id="'+$data.data[$i].id+'" title="Ứng tuyển"><img src="/css/admin/img/global/control-enable.png"></a></td><td>&nbsp;</td></tr>';
													
												}
												
											}
											$('#apply-job-list').html($str);
										}else{
											alert('Không tìm thấy công việc !');
										}
										CANDIDATE.applyInsert();
										CANDIDATE.applyMaster();
									}
								});
								return false;
							});
						CANDIDATE.applyMaster();
					}
				});
				
			});
		},
		addToExport: function(){
			$('a.add-export').unbind('click').click(function(){
				
				var $ob = $(this);
				var $tr = $ob.parent().parent();
				$.ajax({
					type: "POST",
					url: $ob.attr('data-url'),
					data: {
						id: $ob.attr('data-id'),
						action_name: $ob.attr('data-action'),
					},
					cache: false,
					success: function(data){
						$.fancybox.open(data,{
							padding : 10,
							title: $ob.attr('data-title'),
							helpers: {
								overlay:{
									closeClick : false
								} 
							}
						});
						$('div.export-item').unbind('click').click(function(){
							var $ob = $(this);
							$.ajax({
								type: "POST",
								url: $ob.attr('data-url'),
								data: {
									candidate_id: $ob.attr('data-candidate-id'),
									export_id: $ob.attr('data-export-id'),
									action_name: $ob.attr('data-action'),
								},
								cache: false,
								success: function(data){
									$tr.addClass('exported-1');
									$.fancybox.open(data,{
										padding : 10,
										title: $ob.attr('data-title'),
										helpers: {
											overlay:{
												closeClick : false
											}
										}
									});
								}
							});
						});
						$('input.btn-add-export').unbind('click').click(function(){
							var $ob = $(this);
							if($('input#txtExportName').val().trim()==''){
								alert($('input#txtExportName').attr('data-error'));
								$('input#txtExportName').focus().focus();
								return false;
							}
							$.ajax({
								type: "POST",
								url: $ob.attr('data-url'),
								data: {
									candidate_id: $ob.attr('data-candidate-id'),
									action_name: $ob.attr('data-action'),
									txtExport: $('input#txtExportName').val().trim()
								},
								cache: false,
								success: function(data){
									$tr.addClass('exported-1');
									$.fancybox.open(data,{
										padding : 10,
										title: $ob.attr('data-title'),
										helpers: {
											overlay:{
												closeClick : false
											} 
										}
									});
								}
							});
						});
					}
				});
				
			});
		},
		loadInterview: function(){
			var $detail = $('div.candidate-interview', $('tr.dtr_'+CANDIDATE.currentId));
			$detail.html(CANDIDATE.loading).show('fast');
			$.ajax({
				type: "POST",
				url: '/admin/candidates/interview',
				data: {
					id: CANDIDATE.currentId,
					action_name: CANDIDATE.actionNameInterview,
				},
				cache: false,
				success: function(data){
					$detail.html(data);
					CANDIDATE.applyMaster();
				}
			});
		},
		initForm: function(){
			$('select#input_language').unbind('change').change(function(){
				var $ob = $(this);
				$.ajax({
					type: "POST",
					url: '/admin/candidates/getLanguageLevel',
					data: {
						language_id: $ob.val(),
						action_name: CANDIDATE.actionName
					},
					cache: false,
					success: function(data){
						var $js = $.parseJSON(data);
						var $select = $('select#input_language_level');
						if(parseInt($js.status, 10) == 1){
							$select.children('option[value!=-1]').remove();
							for(var i = 0; i < $js.data.length; i++){
								var str = '<option value="'+$js.data[i].id+'">'+$js.data[i].name+'</option>';
								$select.append(str);
							}
						}else{
							alert($js.data);
						}
						
					}
				});
				
			});
			$('select.replace-for').each(function(){
				var $ob = $('#'+$(this).attr('data-replace-id'));
				if($ob){
					if ($ob.val()!=''){
						$(this).val($ob.val());
					}
					$(this).unbind('change').change(function(){
						$ob.val($(this).val());
					});
				}
			});
			
		},
		loadCandidateDetail: function($detail){
			$.ajax({
				type: "POST",
				url: '/admin/candidates/candidateDetails',
				data: {
					id: CANDIDATE.currentId,
					action_name: CANDIDATE.actionName
				},
				cache: false,
				success: function(data){
					$detail.html(data);
					$('.money-show').each(function(){
						$(this).text(MONEY.format($(this).text()));
					});
				}
			});
		},
		searchJob: function(){
			var $detail = $('div.job-search', $('tr.dtr_'+CANDIDATE.currentId));
			$detail.html(CANDIDATE.loading).show('fast');
			$.ajax({
				type: "POST",
				url: '/admin/companies/searchJobs',
				data: {
					candidate_id: CANDIDATE.currentId,
					action_name: CANDIDATE.actionNameJob
				},
				cache: false,
				success: function(data){
					$detail.html(data);
					CANDIDATE.applyInsert();
					CANDIDATE.jobDetail();
				}
			});
		},
		applyInsert: function(){
			$('a.apply-insert').unbind('click').click(function(){
				var $ob = $(this);
				var $str = $('<div></div>');
				
				$.fancybox.close();
				$str.html(CANDIDATE.loading);
				$.fancybox.open($str,{
					padding : 10,
					title: $ob.attr('data-title'),
					width: 800,
					helpers: {
						overlay:{
							closeClick : false
						} 
					}
				});
				$.ajax({
					type: "POST",
					url: '/admin/candidates/apply',
					data: {
						candidate_id: $ob.attr('data-candidate-id'),
						job_id: $ob.attr('data-job-id'),
						action_name: CANDIDATE.actionName
					},
					cache: false,
					success: function(data){
						$.fancybox.close();
						$.fancybox.open($str.html(data),{
							padding : 10,
							title: $ob.attr('data-title'),
							width: 800,
							helpers: {
								overlay:{
									closeClick : false
								} 
							}
						});
						CANDIDATE.applyForm();
					}
				});
			});
		},
		applyMaster: function(){
			
			$('a.apply-edit').unbind('click').click(function(){
				var $ob = $(this);
				var $str = $('<div></div>');
				
				$.fancybox.close();
				$str.html(CANDIDATE.loading);
				$.fancybox.open($str,{
					padding : 10,
					title: $ob.attr('data-title'),
					width: 800,
					helpers: {
						overlay:{
							closeClick : false
						} 
					}
				});
				$.ajax({
					type: "POST",
					url: '/admin/candidates/apply',
					data: {
						candidate_id: $ob.attr('data-candidate-id'),
						job_id: $ob.attr('data-job-id'),
						action_name: CANDIDATE.actionName
					},
					cache: false,
					success: function(data){
						$.fancybox.close();
						$.fancybox.open($str.html(data),{
							padding : 10,
							title: $ob.attr('data-title'),
							width: 800,
							helpers: {
								overlay:{
									closeClick : false
								} 
							}
						});
						CANDIDATE.applyForm();
					}
				});
			});
			$('a.apply-delete').unbind('click').click(function(){
				var $ob = $(this);
				if(confirm($ob.attr('data-confirm'))){
					$.ajax({
						type: "POST",
						url: '/admin/candidates/apply',
						data: {
							id: $ob.attr('data-id'),
							action_name: CANDIDATE.actionNameDelete
						},
						cache: false,
						success: function(data){
							var js = $.parseJSON(data);
							alert(js.data);
							if(parseInt(js.status) == 1){
								$ob.parent().parent().remove();
							}
						}
					});
				}
			});
		},
		
		applyForm: function(){
			$('input.date-picker-interview').datepicker({minDate: $('#date_apply').val()});
			$('span.error').hide();
			$('input#button-submit').click(function(){
				$('span.error').hide();
				var $ob = $(this);
				var $dateInterview = $('#CandidateToJobs_date_interview');
				var $note = $('#CandidateToJobs_note_description');
				var $result = $('select#CandidateToJobs_result_status');//children('option:selected').text()
				var $comment = $('#CandidateToJobs_comment_from_employer');
				var $candidate = $('#CandidateToJobs_candidate_id');
				var $job = $('#CandidateToJobs_job_id');
				
				/*if($dateInterview.val()==''){
					$dateInterview.next('span.error').show();
					$dateInterview.focus();
					$dateInterview.focus();
					return false;
				}*/
				$.ajax({
					type: "POST",
					url: '/admin/candidates/apply',
					data: {
						candidate_id: $candidate.val(),
						job_id: $job.val(),
						note_description: $note.val(),
						date_interview: $dateInterview.val(),
						comment_from_employer:  $comment.val(),
						result_status: $result.children('option:selected').val(),
						
						action_name: $ob.attr('data-action')
					},
					cache: false,
					success: function(data){
						//alert(data);return;
						var js = $.parseJSON(data);
						alert(js.data);
						if(parseInt(js.status)==1){
							$.fancybox.close();
							var $tr = $('tr.dtr_'+CANDIDATE.currentId);
							var $divDetail = $('div.candidate-detail-information', $tr);
							var $divSeach = $('div.job-search', $tr);
							$divDetail.html(CANDIDATE.loading);
							$divSeach.html('').hide();
							CANDIDATE.loadCandidateDetail($divDetail);
						}
					}
				});
			});
			
		},
		jobDetail: function (){
			$('a.job-detail').unbind('click').click(function(){
				var $ob = $(this);
				var $str = $('<div></div>');
				$str.html(CANDIDATE.loading);
				$.fancybox.open($str,{
					padding : 7,
					title: $ob.attr('data-title'),
					width: 800,
					helpers: {
						overlay:{
							closeClick : false
						} 
					}
				});
				
				$.ajax({
					type: "POST",
					url: '/admin/companies/jobDetails',
					data: {
						id: $ob.attr('data-id'),
						action_name: CANDIDATE.actionNameJob
					},
					cache: false,
					success: function(data){
						var strControl = '<div class="dt-control">'
							+'<a href="#" class="apply-insert-in-detail" data-title="'+$ob.attr('data-apply')+'" data-candidate-id="'+CANDIDATE.currentId+'" data-job-id="'+$ob.attr('data-id')+'">'
							+$ob.attr('data-apply')
							+'</a>';
							+'</div>';
						$.fancybox.close();
						$.fancybox.open($str.html(strControl).append(data).append(strControl),{
							padding : 10,
							title: $ob.attr('data-title'),
							width: 800,
							helpers: {
								overlay:{
									closeClick : false
								} 
							}
						});
						CANDIDATE.applyJobDetail();
						
					}
				});
				
				
			});
		},
		applyJobDetail: function(){
			$('a.apply-edit-in-job, a.apply-insert-in-detail').unbind('click').click(function(){
				var $ob = $(this);
				var $str = $('<div></div>');
				
				$.fancybox.close();
				$str.html(CANDIDATE.loading);
				$.fancybox.open($str,{
					padding : 10,
					title: $ob.attr('data-title'),
					width: 800,
					helpers: {
						overlay:{
							closeClick : false
						} 
					}
				});
				$.ajax({
					type: "POST",
					url: '/admin/candidates/apply',
					data: {
						candidate_id: $ob.attr('data-candidate-id'),
						job_id: $ob.attr('data-job-id'),
						action_name: CANDIDATE.actionName
					},
					cache: false,
					success: function(data){
						$.fancybox.close();
						$.fancybox.open($str.html(data),{
							padding : 10,
							title: $ob.attr('data-title'),
							width: 800,
							helpers: {
								overlay:{
									closeClick : false
								} 
							}
						});
						CANDIDATE.applyForm();
					}
				});
			});
			$('a.apply-delete-in-job').unbind('click').click(function(){
				var $ob = $(this);
				if(confirm($ob.attr('data-confirm'))){
					$.ajax({
						type: "POST",
						url: '/admin/candidates/apply',
						data: {
							id: $ob.attr('data-id'),
							action_name: CANDIDATE.actionNameDelete
						},
						cache: false,
						success: function(data){
							var js = $.parseJSON(data);
							alert(js.data);
							if(parseInt(js.status) == 1){
								$ob.parent().parent().remove();
							}
						}
					});
				}
			});
		},
		//FORM
		
		
		addChildMajor: function(){
			$('a.add-child-major').unbind('click').click(function(){
				var $oYear = $('input#input_major_year');
				var $oMajorId = $('select#input_major');
				var $oWorkDes = $('textarea#input_major_description');
				
				
				if($oMajorId.val()=='-1'){
					alert($oMajorId.attr('data-error'));
					$oMajorId.focus();
					$oMajorId.focus();
					return false;
				};
				
				
				var addInput = $('<input name="majors[]" type="hidden" value=""/>');
				var addInput2 = $('<input name="major_des[]" type="hidden" value=""/>');
				addInput.val($oMajorId.children('option:selected').val()+';'+$oYear.val());
				addInput2.val($oWorkDes.val());
				
				var controlTd = $('<td class="child-control"></td>')
					.append('<a href="#" class="remove-child" title="">&nbsp;</a>')
					.append(addInput)
					.append(addInput2);
				var element = $('<tr></tr>')
						.append('<td class="child">'+(parseInt($oMajorId.children('option:selected').val())>0?$oMajorId.children('option:selected').text():'&nbsp;')+'</td>')
						.append('<td class="child">'+$oYear.val()+'</td>')
						.append('<td class="child">'+$oWorkDes.val()+'</td>')
						
						
						.append(controlTd);
				$(this).parent().parent().before(element);
				$oMajorId.val('-1');
				$oYear.val('');
				$oWorkDes.val('');
				CANDIDATE.removeChild();
				return false;
			});
			CANDIDATE.removeChild();
			return false;
		},
		addChildSkill: function(){
			$('a.add-child-skill').unbind('click').click(function(){
				var $oSkill = $('input#input_skill');
				var $oDes = $('textarea#input_skill_description');
				
				
				if($oSkill.val()==''){
					alert($oSkill.attr('data-error'));
					$oSkill.focus();
					$oSkill.focus();
					return false;
				};
				
				
				var addInput = $('<input name="skill[]" type="hidden" value=""/>');
				var addInput2 = $('<input name="skill_des[]" type="hidden" value=""/>');
				addInput.val($oSkill.val());
				addInput2.val($oDes.val());
				
				var controlTd = $('<td class="child-control"></td>')
					.append('<a href="#" class="remove-child" title="">&nbsp;</a>')
					.append(addInput)
					.append(addInput2);
				var element = $('<tr></tr>')
						.append('<td class="child">'+$oSkill.val()+'</td>')
						.append('<td class="child">'+$oDes.val()+'</td>')
						
						
						.append(controlTd);
				$(this).parent().parent().before(element);
				$oSkill.val('');
				$oDes.val('');
				CANDIDATE.removeChild();
				return false;
			});
			CANDIDATE.removeChild();
			return false;
		},
		
		addChildLanguage: function(){
			$('a.add-child-language').unbind('click').click(function(){
				var $oLanguage = $('select#input_language');
				var $oLevel = $('select#input_language_level');
				var $oWorkDes = $('textarea#input_language_description');
				
				
				if($oLanguage.val()=='-1'){
					alert($oLanguage.attr('data-error'));
					$oLanguage.focus();
					$oLanguage.focus();
					return false;
				};
				
				if($oLevel.val()=='-1'){
					alert($oLevel.attr('data-error'));
					$oLevel.focus();
					$oLevel.focus();
					return false;
				};
				
				var addInput = $('<input name="languages[]" type="hidden" value=""/>');
				var addInput2 = $('<input name="language_des[]" type="hidden" value=""/>');
				addInput.val($oLanguage.children('option:selected').val()+';'+$oLevel.children('option:selected').val());
				addInput2.val($oWorkDes.val());
				
				var controlTd = $('<td class="child-control"></td>')
					.append('<a href="#" class="remove-child" title="">&nbsp;</a>')
					.append(addInput)
					.append(addInput2);
				var element = $('<tr></tr>')
						.append('<td class="child">'+$oLanguage.children('option:selected').text()+'</td>')
						.append('<td class="child">'+$oLevel.children('option:selected').text()+'</td>')
						.append('<td class="child">'+$oWorkDes.val()+'</td>')
						
						
						.append(controlTd);
				$(this).parent().parent().before(element);
				$oLanguage.val('-1');
				$oLevel.val('-1');
				$oWorkDes.val('');
				CANDIDATE.removeChild();
				return false;
			});
			CANDIDATE.removeChild();
			return false;
		},
		addChildTechnology: function(){
			$('a.add-child-technology').unbind('click').click(function(){
				var $oLanguage = $('select#input_technology');
				var $oLevel = $('select#input_technology_level');
				var $oWorkDes = $('textarea#input_technology_description');
				
				
				if($oLanguage.val()=='-1'){
					alert($oLanguage.attr('data-error'));
					$oLanguage.focus();
					$oLanguage.focus();
					return false;
				};
				
				if($oLevel.val()=='-1'){
					alert($oLevel.attr('data-error'));
					$oLevel.focus();
					$oLevel.focus();
					return false;
				};
				
				var addInput = $('<input name="technologies[]" type="hidden" value=""/>');
				var addInput2 = $('<input name="technology_des[]" type="hidden" value=""/>');
				addInput.val($oLanguage.children('option:selected').val()+';'+$oLevel.children('option:selected').val());
				addInput2.val($oWorkDes.val());
				
				var controlTd = $('<td class="child-control"></td>')
					.append('<a href="#" class="remove-child" title="">&nbsp;</a>')
					.append(addInput)
					.append(addInput2);
				var element = $('<tr></tr>')
						.append('<td class="child">'+$oLanguage.children('option:selected').text()+'</td>')
						.append('<td class="child">'+$oLevel.children('option:selected').text()+'</td>')
						.append('<td class="child">'+$oWorkDes.val()+'</td>')
						
						
						.append(controlTd);
				$(this).parent().parent().before(element);
				$oLanguage.val('-1');
				$oLevel.val('-1');
				$oWorkDes.val('');
				CANDIDATE.removeChild();
				return false;
			});
			CANDIDATE.removeChild();
			return false;
		},
		removeChild: function(){
			$('a.remove-child').unbind('click').click(function(){
				$(this).parent().parent().remove();
			});
		}
}